import registnet.dao.*;

int key=0;
int hasError = 0;
boolean showInfo=false;
Vector vcourses = new Vector();
Vector vprereqs = new Vector();
String oldVal = new String();
List curricula;
HashMap prereqmap= new HashMap();

int cId;
		void checkIfCurrCourseExist(){
			cId = Integer.parseInt(curriculum_id.getValue());
			if(!(new CurriculumCoursePeer()).hasCurriculumCourse(cId)){
				showInfo =false;
				importM.setVisible(true);
				addYear();
				addSemester();
			}
			else{
				showInfo=true;
				showInfoCurriculumCourse(cId);
			}
		}


		void addYear(){
			Row row = new Row();
			Vbox vb = new Vbox();
			Label yearLabel = new Label();
			vb.setSpacing("20px");
			vb.setAlign("center");
			vb.setParent(row);
			vb.setWidth("95%");
			row.setParent(yearRow);
			yearLabel.setValue(getOrder(yearRow.getChildren().size())+" Year");
			yearLabel.setParent(vb);
			vb.setId("year_"+yearRow.getChildren().size());
			gridYear.setActivePage(yearRow.getChildren().size()-1);
		}
		
		void addSemester(){
			int currentYear = gridYear.getActivePage()+1;
			Vbox vb = yearRow.getFellow("year_"+currentYear);
			Vbox sem = new Vbox();
			
			int numSemester = vb.getChildren().size();
			Label semLabel = new Label();
			if(numSemester<=2)
				semLabel.setValue(getOrder(numSemester) + " Semester");
			else
				semLabel.setValue("Summer");
			semLabel.setParent(sem);
			
			Listbox lb = new Listbox();
			lb.setWidth("540px");
			lb.setStyle("padding: 1px; margin: 0 auto auto;");
			
			Listhead lh = new Listhead();
			Listheader lhrc = new Listheader();
			Listheader lhru = new Listheader();
			Listheader lhrp = new Listheader();
			
			lhrc.setLabel("Courses");
			lhrp.setLabel("Prerequisites");
			lhru.setLabel("Units");
			
			lhrc.setWidth("35%");
			lhrp.setWidth("50%");
			lhru.setWidth("10%");
			
			
			
			lhrc.setParent(lh);
			lhrp.setParent(lh);
			lhru.setParent(lh);
			lh.setParent(lb);
			lb.setParent(sem);
			
			lb.setId("semester_"+currentYear+"_"+numSemester);
			sem.setSpacing("0px");
			
			
			
			Div div = new Div();
			if(showInfo)
				div.setVisible(false);
				
			div.setWidth("100%");
			Menubar footer = new Menubar();
			Menuitem add = new Menuitem();
			Menuitem delete = new Menuitem();
			
			div.setAlign("center");
			add.setLabel("add");
			delete.setLabel("delete");
			add.addEventListener("onClick", new ClickEventListener("addCourse",lb));
			delete.addEventListener("onClick", new ClickEventListener("deleteCourse",lb));
			
			div.setParent(sem);
			footer.setParent(div);
			add.setParent(footer);
			delete.setParent(footer);
			Hbox hb = new Hbox();
			hb.setSpacing("56%");
			Button addSem = new Button();
			Hbox h = new Hbox();
			Label t_units = new Label();
			Label t_unitsValue = new Label();
			if(!showInfo){
				if(numSemester<2){
					addSem.setLabel("Add Semester");
					addSem.addEventListener("onClick", new ClickEventListener("addSem", addSem));
					addSem.setParent(hb);
				}
				else if(numSemester==2){
					addSem.setLabel("Add Summer");
					addSem.addEventListener("onClick", new ClickEventListener("addSem", addSem));
					addSem.setParent(hb);
				}
				t_units.setParent(h);
				t_units.setValue("Semester Units: ");
				t_unitsValue.setParent(h);
				t_unitsValue.setValue("0");
				t_unitsValue.setId(currentYear+"_"+numSemester);
				h.setParent(hb);
				hb.setParent(sem);
			}
			sem.setParent(vb);
			addCourse(lb);
			
		}
		
		void addCourse(Listbox lb){
			Listitem li = new Listitem();
			Listcell lcc = new Listcell();
			Listcell lcp = new Listcell();
			Listcell lcu = new Listcell();
			Object courses;
			if(!showInfo)
				courses = new Textbox();
			else	
				courses = new Label();
			Label units = new Label();
			units.setValue("0");
			
			//Component prereq = Executions.createComponents("/WEB-INF/macros/curriculaPrereq.zul", null, null);
			Object prereq;
			
			prereq = new Label();
			if(!showInfo){
				prereq.setValue("NONE");
			}
			Hbox hb = new Hbox();
			hb.setValign("center");
			Label error = new Label();
			error.setVisible(false);
			error.setValue("not found");
			error.setStyle("font-size: 10px; color: red");
			
			courses.setParent(hb);
			
			error.setParent(hb);
			hb.setParent(lcc);
			if(!showInfo){
				courses.addEventListener("onBlur", new BlurEventListener(courses,error));
				courses.addEventListener("onBlur", new AddPrereqEventListener(prereq));
				courses.addEventListener("onFocus", new FocusEventListener(courses));
			}
			units.setParent(lcu);
			prereq.setParent(lcp);
			
			String addId = (gridYear.getActivePage()+1)+"_"+lb.getId().charAt(lb.getId().length()-1)+"_"+ (lb.getChildren().size());
			courses.setId("subject_"+addId);
			prereq.setId("prereq_"+addId);
			units.setId("units_"+addId);
			error.setId("error_"+addId);
			lcc.setParent(li);
			lcp.setParent(li);
			lcu.setParent(li);
			
			li.setParent(lb);
		}
		
		deleteCourse(Listbox lb){
			int selected = lb.getSelectedIndex();
			if(selected!=-1){
				int size = lb.getChildren().size()-1;
				int year = gridYear.getActivePage()+1;
				String sem = (lb.getId().split("_"))[2];
				String addId = "_"+year+"_"+sem+"_";
				String oldVal = lb.getFellow("subject"+addId+""+(selected+1)).getValue().toUpperCase();
				if(!oldVal.contains("NSTP")){
					int un = Integer.parseInt(lb.getFellow("units"+addId+""+(selected+1)).getValue()); 
					total_units.setValue((Integer.parseInt(total_units.getValue()) - un)+"");
					Label sem_units = yearRow.getFellow(year+"_"+sem);
					sem_units.setValue((Integer.parseInt(sem_units.getValue())-un)+"");
				}
				prereqmap.remove(oldVal);
				if(oldVal.toUpperCase().contains("(MST)"))
					total_MST.setValue((Integer.parseInt(total_MST.getValue()) - 1)+"");
				else if(oldVal.toUpperCase().contains("(AH)"))
					total_AH.setValue((Integer.parseInt(total_AH.getValue()) - 1)+"");
				else if(oldVal.toUpperCase().contains("(SSP)"))
					total_SSP.setValue((Integer.parseInt(total_SSP.getValue()) - 1)+"");
				else if(oldVal.toUpperCase().contains("ELECTIVE"))
					total_ELECTIVE.setValue((Integer.parseInt(total_ELECTIVE.getValue()) - 1)+"");		
				for(int i=0; i<vprereqs.size(); i++){
					if(vprereqs.elementAt(i).contains(oldVal) && !oldVal.equals("") && oldVal!=null){
						String s = (String) vprereqs.elementAt(i);
						String[] id = s.split("__");
						Label le = lb.getFellow(id[1].replace("subject", "error"));
						if(!le.isVisible()){
							le.setVisible(true);
							hasError++;
						}
						else{
							if(hasError>0)
								hasError--;
						}
					}
					else if(vprereqs.elementAt(i).contains("subject"+addId+""+(selected+1))){
						vprereqs.remove(i);
						i--;
					}
				}
				for(int k=0; k<vcourses.size(); k++){
					if(vcourses.elementAt(k).contains(oldVal)){
						vcourses.remove(k);
						k--;
					}
				}
				if(lb.getFellow("error"+addId+(selected+1)).isVisible()){
					if(hasError>0)
						hasError--;
				}
				lb.removeItemAt(selected);
				if((size-1) != selected){
					for(int i=selected+2; i<=size; i++){
						Textbox course = gridYear.getFellow("subject"+addId+i);
						Label prereq = lb.getFellow("prereq"+addId+i);
						Label units = lb.getFellow("units"+addId+i);
						Label error = lb.getFellow("error"+addId+i);
						
						course.setId("subject"+addId+(i-1));
						prereq.setId("prereq"+addId+(i-1));
						units.setId("units"+addId+(i-1));
						error.setId("error"+addId+(i-1));
						
						for(int j=0; j<vprereqs.size(); j++){
							if(vprereqs.elementAt(j).contains("subject"+addId+i))
								vprereqs.set(j, vprereqs.get(j).replace("subject"+addId+i, "subject"+addId+(i-1)));
						}
					}
				}
			}
		}
		
		String getOrder(int n){
			if(n==1)
				headerValue = n + "st";
			else if(n==2)
				headerValue = n + "nd";
			else if(n==3)
				headerValue = n + "rd";
			else
				headerValue = n + "th";
			
			return headerValue;
		}
		
		
		/*retrievePrereqItems(Listbox lb, Label bb){
			String courseid = bb.getId().replace("prereq", "subject");
			String courseCode = bb.getFellow(courseid).getValue();
			String[] l;
			
			if(!courseCode.equals("")){
				Course course = (new CoursePeer()).getCourse(courseCode);
				Vector pre = new Vector();
				if(course!=null){
					String prerequisites = grammar.Converter.prefixToInfix(course.getPrerequisite());
					
					if(!prerequisites.equals("") && !prerequisites.equals("NONE")){
						String[] date = bb.getId().split("_");
						int year = Integer.parseInt(date[1]);
						int sem = Integer.parseInt(date[2]); 
						l = extractPrereq(prerequisites);
						if(l!=null){
							for(int i=0; i<l.length; i++){
								if(!l[i].contains(" AND ")){
									for(int j=0; j<vcourses.size(); j++){
										if(vcourses.elementAt(j).contains(l[i])){
											String[] pdate = vcourses.elementAt(j).split("_");
											int pyear = Integer.parseInt(pdate[1]);
											int psem = Integer.parseInt(pdate[2]); 
											if(pyear<year || (pyear==year&&psem<sem)){
												pre.add(l[i]);
												bb.setValue(l[i]);
												break;
											}
										}
									}
								}
								else{
									String[] ands = l[i].split(" AND "); 
									boolean b = false;
									for(int j=0; j<ands.length; j++){
										for(int k=0; k<vcourses.size(); k++){
											if(vcourses.elementAt(k).contains(ands[j])){
												String[] pdate = vcourses.elementAt(k).split("_");
												int pyear = Integer.parseInt(pdate[1]);
												int psem = Integer.parseInt(pdate[2]); 
												if(pyear<year || (pyear==year&&psem<sem)){
													b = true;
													break;
												}
												else
													b = false;
											}
											else
												b = false;
										}
										if(!b)
											break;
									}
									if(b){
										pre.add(l[i]);
										bb.setValue(l[i]);
										break;
									}
								}	
							}
							
						}
						else{
							for(int k=0; k<vcourses.size(); k++){
								if(vcourses.elementAt(k).contains(prerequisites)){
									String[] pdate = vcourses.elementAt(k).split("_");
									int pyear = Integer.parseInt(pdate[1]);
									int psem = Integer.parseInt(pdate[2]); 
									if(pyear<year || (pyear==year&&psem<sem)){
										pre.add(prerequisites);
										bb.setValue(prerequisites);
										break;
									}
								}
							}
						}
					}
					else{
						pre.add(prerequisites);
						bb.setValue(prerequisites);
					}
				}
			}
		}
		*/
		
		String[] extractPrereq(String course){
			String[] prereq = course.split(" OR ");
			return prereq;
		}
		
		
		
		retrievePrereqItems(Listbox lb, Label bb){
			String courseid = bb.getId().replace("prereq", "subject");
			String courseCode = bb.getFellow(courseid).getValue();
			String[] l;
			
			if(!courseCode.equals("")){
				Course course = (new CoursePeer()).getCourse(courseCode);
				Vector pre = new Vector();
				if(course!=null){
					String prerequisites = grammar.Converter.prefixToInfix(course.getPrerequisite());
					if(!prerequisites.equals("(DUMMY)"))
						bb.setValue(prerequisites);
				}
			}
		}
		addCurriculumCourse(){
			Vector courses = new Vector();
			Vector prereqs = new Vector();
			boolean hasErr = false;
			
			for(int i=1; i<=yearRow.getChildren().size(); i++){
				int semSize = yearRow.getFellow("year_"+i).getChildren().size()-1;   
				for(int j=1; j<=semSize; j++){
					int courseSize = yearRow.getFellow("semester_"+i+"_"+j).getChildren().size()-1;
					for(int k=1; k<=courseSize; k++){
						String addId = "_"+i+"_"+j+"_"+k;
						int course = (new CoursePeer()).getCourseId(yearRow.getFellow("subject"+addId).getValue());
						String prereq = yearRow.getFellow("prereq"+addId).getValue();
						if(yearRow.getFellow("subject"+addId).equals("")){
							hasErr = true;
							break;
						}
						courses.add(course+"_"+i+"_"+j);
						prereqs.add(prereq);
					}
				}
			}
			
			if(!hasErr && hasError==0){
				int size = courses.size();
				
				CurriculumCoursePeer curCourse = new CurriculumCoursePeer();  
				Curriculum curri = (new CurriculumPeer()).getCurriculum(Integer.parseInt(curriculum_id.getValue()));
				(new CurriculumPeer()).updateCurriculum(curri.getCurriculumId(),Integer.parseInt(total_units.value),curri.getAHCount(),curri.getMSTCount(),curri.getSSPCount(),curri.getElectiveCount());
				for(int i=0; i<size; i++){
					String[] data = courses.get(i).split("_");
					curCourse.createNewCurriculumCourse(Integer.parseInt(curriculum_id.getValue()), Integer.parseInt(data[0]), Integer.parseInt(data[2]), Integer.parseInt(data[1]), prereqs.get(i));
				}
				(new CurriculumPeer()).updateCurriculum(curri.getCurriculumId(),Integer.parseInt(total_units.value),Integer.parseInt(total_AH.value),Integer.parseInt(total_MST.value),Integer.parseInt(total_SSP.value), Integer.parseInt(total_ELECTIVE.value));
				title.setValue(title.getValue() + " was successfully configured.");
				instance_tree.clearSelection();
				contents.removeChild(contents.getFirstChild());
				
			}
			else
				alert("Wrong input of course code");
			
		}
		
		
		
		
		public class ItemRenderer implements ListitemRenderer {
			public void render(Listitem item, java.lang.Object data) {
				new Listcell(data).setParent(item);
			}
		}
		
		private class ClickEventListener implements EventListener {
		String submitType;
		Object b;
			public ClickEventListener (String submitType, Object b){
				this.submitType = submitType; 
				this.b = b;
			}
				
			public void onEvent(Event event) throws UiException {
				if(this.submitType.equals("addYear"))
					addYear();
				else if (this.submitType.equals("addSem")){
					addSemester();
					((Button)b).detach();
				}
				else if(this.submitType.equals("addCourse"))
					addCourse((Listbox)b);
				else if(this.submitType.equals("deleteCourse"))
					deleteCourse((Listbox)b);
			}
			
		}
		
		
		private class AddCourseEventListener implements EventListener {
		Listbox lb;
		Button b;
		public AddCourseEventListener (Listbox lb, Button b){
			this.lb = lb;
			this.b = b; 
		}
			
		public void onEvent(Event event) throws UiException {
			addCourse(lb);
			b.detach();
		}
		}
		
		private class AddPrereqEventListener implements EventListener {
		Listbox lb;
		Label tb;
		public AddPrereqEventListener (Label tb){
			this.lb = lb;
			this.tb = tb; 
		}
			
		public void onEvent(Event event) throws UiException {
			retrievePrereqItems(prereq_lb,tb);
		}
		}
		
		private class FocusEventListener implements EventListener {
		Textbox tb;
		public FocusEventListener (Textbox tb){
			this.tb = tb; 
		}
			
		public void onEvent(Event event) throws UiException {
			oldVal = tb.getValue();
		}
		}
		
		
		private class BlurEventListener implements EventListener {
			Textbox courseCode;
			Label l;
			public BlurEventListener (Textbox courseCode, Label l){
				this.courseCode = courseCode;
				this.l = l;
			}
				
			public void onEvent(Event event) throws UiException {
				boolean duplicate = false;
				for(int k=0; k<vcourses.size(); k++){
					if(vcourses.elementAt(k).contains(courseCode.getValue()+"_") && !courseCode.getValue().equals("")){
						duplicate=true;
						break;
					}
				}
				Course course1 = (new CoursePeer()).getCourse(oldVal);
				if(course1!=null){
					if(!oldVal.toUpperCase().equals(courseCode.getValue().toUpperCase()) && !oldVal.equals("")){
						for(int i=0; i<vprereqs.size(); i++){
							
							if(vprereqs.elementAt(i).toUpperCase().contains(oldVal.toUpperCase())){
								String s = (String) vprereqs.elementAt(i);
								String[] id = s.split("__");
								Label le = courseCode.getFellow(id[1].replace("subject", "error"));
								if(!le.isVisible()){
									le.setVisible(true);
									hasError++;
								}
							}
							if(vprereqs.elementAt(i).contains(courseCode.getId())){
								vprereqs.remove(i);
								i--;
							}
						}
						for(int k=0; k<vcourses.size(); k++){
							if(vcourses.elementAt(k).contains(oldVal.toUpperCase()+"_")){
								vcourses.remove(k);
								k--;
							}
						}
						String unitsId = courseCode.getId().replace("subject", "units");
						if(oldVal.toUpperCase().contains("(MST)")){
							total_MST.setValue((Integer.parseInt(total_MST.getValue()) - 1)+"");
						}
						else if(oldVal.toUpperCase().contains("(AH)")){
							total_AH.setValue((Integer.parseInt(total_AH.getValue()) - 1)+"");
						}
						else if(oldVal.toUpperCase().contains("(SSP)")){
							total_SSP.setValue((Integer.parseInt(total_SSP.getValue()) - 1)+"");
						}
						else if(oldVal.toUpperCase().contains("ELECTIVE")){
							total_ELECTIVE.setValue((Integer.parseInt(total_ELECTIVE.getValue()) - 1)+"");
						}
						prereqmap.remove(oldVal);
					}
				}
				if(!duplicate && !courseCode.getValue().equals("")){
					if(!oldVal.equals(courseCode.getValue())){
						Course course = (new CoursePeer()).getCourse(courseCode.getValue().toUpperCase());
						if(course==null){
							String unitsId = courseCode.getId().replace("subject", "units");
							String[] semUnits = unitsId.split("_");
							total_units.setValue(Integer.parseInt(total_units.getValue())-Integer.parseInt(yearRow.getFellow(unitsId).getValue())+"");
							Label sem_units = yearRow.getFellow(semUnits[1]+"_"+semUnits[2]);
							sem_units.setValue(Integer.parseInt(sem_units.getValue())-Integer.parseInt(yearRow.getFellow(unitsId).getValue())+"");
							
							yearRow.getFellow(unitsId).setValue("0");
							if(!l.isVisible()){
								l.setVisible(true);
								hasError++;
							}
							
						}
						else{
							String prereq = grammar.Converter.prefixToInfix(course.getPrerequisite());
							if(prereq.equals("NONE") || prereq.equals("") || prereq.equals("(DUMMY)") || prereq.contains("GRADUATE")){
								if(!vcourses.contains(courseCode.getId().replace("subject",course.getCourseCode())))
									if(!(course.getCourseCode().equals("GE(MST)") || course.getCourseCode().equals("GE(SSP)") || course.getCourseCode().equals("GE(AH)") || course.getCourseCode().equals("ELECTIVE")))
										vcourses.add(courseCode.getId().replace("subject",course.getCourseCode()));
								
								
								if(l.isVisible()){
									l.setVisible(false);
									if(hasError>0)
										hasError--;
								}
								String unitsId = courseCode.getId().replace("subject", "units");
								
								if(!course.getCourseCode().contains("NSTP")){
									total_units.setValue(Integer.parseInt(total_units.getValue())-Integer.parseInt(yearRow.getFellow(unitsId).getValue())+"");
									total_units.setValue(Integer.parseInt(total_units.getValue())+course.getUnits()+"");
									String[] semUnits = unitsId.split("_");
									Label sem_units = yearRow.getFellow(semUnits[1]+"_"+semUnits[2]);
									
									sem_units.setValue(Integer.parseInt(sem_units.getValue())-Integer.parseInt(yearRow.getFellow(unitsId).getValue())+"");
									sem_units.setValue(Integer.parseInt(sem_units.getValue())+course.getUnits()+"");
								}
								if(courseCode.getValue().toUpperCase().contains("(MST)")){
									total_MST.setValue(Integer.parseInt(total_MST.getValue())+1+"");
								}
								else if(courseCode.getValue().toUpperCase().contains("(AH)")){
									total_AH.setValue(Integer.parseInt(total_AH.getValue())+1+"");
								}
								else if(courseCode.getValue().toUpperCase().contains("(SSP)")){
									total_SSP.setValue(Integer.parseInt(total_SSP.getValue())+1+"");
								}
								else if(courseCode.getValue().toUpperCase().contains("ELECTIVE")){
									total_ELECTIVE.setValue(Integer.parseInt(total_ELECTIVE.getValue())+1+"");
								}
							
								
								yearRow.getFellow(unitsId).setValue(course.getUnits()+"");
								for(int i=0; i<vprereqs.size(); i++){
									if(vprereqs.elementAt(i).contains(course.getCourseCode())){
										String s = (String) vprereqs.elementAt(i);
										String[] id = s.split("__");
										Label le = courseCode.getFellow(id[1].replace("subject", "error"));
										Label lpre = courseCode.getFellow(id[1].replace("subject", "prereq"));
										String[] date = id[1].split("_");
										int year = Integer.parseInt(date[1]);
										int sem = Integer.parseInt(date[2]); 
										String prer =  prereqmap.get(courseCode.getFellow(id[1]).getValue().toUpperCase());
										
										if(!prer.contains(" AND ")){
											if(le.isVisible()){
												le.setVisible(false);
												if(hasError>0)
													hasError--;
											}
										}
										else{
											String[] ands = prer.split(" AND "); 
											boolean b = false;
											for(int j=0; j<ands.length; j++){
												if(ands[j].equals("SENIOR")){
													if(year>=4)
														b=true;
													else{
														b=false;
														break;
													}
												}
												else if(ands[j].equals("JUNIOR")){
													if(year>=3)
														b=true;
													else{
														b=false;
														break;
													}
												}
												else if(ands[j].equals("SOPHOMORE")){
													if(year>=2)
														b=true;
													else{
														b=false;
														break;
													}
												}
												else{
													for(int k=0; k<vcourses.size(); k++){
														if(vcourses.elementAt(k).contains(ands[j]+"_")){
															String[] pdate = vcourses.elementAt(k).split("_");
															int pyear = Integer.parseInt(pdate[1]);
															int psem = Integer.parseInt(pdate[2]); 
															if(pyear<year || (pyear==year&&psem<sem)){
																b = true;
																break;
															}
															else
																b = false;
														}
														else
															b = false;
													}
													if(!b)
														break;
												}
											}
											if(b){
												if(le.isVisible()){
													le.setVisible(false);
													if(hasError>0)
														hasError--;
												}
											}
										}
									}
								}
							} //if prereq == NONE
							else{
								
								String[] date = courseCode.getId().split("_");
								int year = Integer.parseInt(date[1]);
								int sem = Integer.parseInt(date[2]); 
								String[] prereqs = extractPrereq(prereq);
								boolean satisfied = false;
								boolean coiAnd = false;
								if(prereq.contains("COI")){
									String coi = new String();
									for(int i=0; i<prereqs.length; i++){
										if(prereqs[i].contains("COI")){
											coi = prereqs[i];
											break;
										}
									}
									if(!coi.contains(" AND ")){
										satisfied=true;
									}
									else
										coiAnd=true;
								}
								else{
									coiAnd=true;
								}
								if(prereqs!=null && coiAnd){
									for(int i=0; i<prereqs.length; i++){
										if(!prereqs[i].contains(" AND ")){
											if(prereqs[i].equals("SENIOR") && year>=4){
												satisfied=true;
												break;
											}
											else if(prereqs[i].equals("JUNIOR") && year>=3){
												satisfied=true;
												break;
											}
											else if(prereqs[i].equals("SOPHOMORE") && year>=2){
												satisfied=true;
												break;
											}
											else{	
												for(int j=0; j<vcourses.size(); j++){
													if(vcourses.elementAt(j).contains(prereqs[i]+"_")){
														String[] pdate = vcourses.elementAt(j).split("_");
														int pyear = Integer.parseInt(pdate[1]);
														int psem = Integer.parseInt(pdate[2]); 
														if(pyear<year || (pyear==year&&psem<sem)){
															if(!vprereqs.contains(prereqs[i]+"__"+courseCode.getId()))
																vprereqs.add(prereqs[i]+"__"+courseCode.getId());
															satisfied=true;
															break;
														}
													}
												}
												if(satisfied){
													prereqmap.put(course.getCourseCode(),prereqs[i]);
													break;
												}
											}
										}
										else{
											String[] ands = prereqs[i].split(" AND "); 
											boolean b = false;
											for(int j=0; j<ands.length; j++){
												if(ands[j].equals("SENIOR")){
													if(year>=4)
														b=true;
													else{
														b=false;
														break;
													}
												}
												else if(ands[j].equals("JUNIOR")){
													if(year>=3)
														b=true;
													else{
														b=false;
														break;
													}
												}
												else if(ands[j].equals("SOPHOMORE")){
													if(year>=2)
														b=true;
													else{
														b=false;
														break;
													}
												}
												else if(ands[j].equals("COI")){
													b=true;
												}
												else{
													for(int k=0; k<vcourses.size(); k++){
														if(vcourses.elementAt(k).contains(ands[j]+"_")){
															String[] pdate = vcourses.elementAt(k).split("_");
															int pyear = Integer.parseInt(pdate[1]);
															int psem = Integer.parseInt(pdate[2]); 
															if(pyear<year || (pyear==year&&psem<sem)){
																b = true;
																break;
															}
															else
																b = false;
														}
														else
															b = false;
													}
													if(!b)
														break;
												}
											}
											if(b){
												satisfied=true;
												
												for(int j=0; j<ands.length; j++){
													if(!vprereqs.contains(ands[j]+"__"+courseCode.getId()) && !(ands[j].equals("SENIOR") || ands[j].equals("JUNIOR") || ands[j].equals("SOPHOMORE") || ands[j].equals("COI")))
														vprereqs.add(ands[j]+"__"+courseCode.getId());
												}
												prereqmap.put(course.getCourseCode(),prereqs[i]);
												break;
											}
										}	
									}
								}
								if(!satisfied){
									if(!l.isVisible()){
										l.setVisible(true);
										hasError++;
									}
								}
								else{
									if(l.isVisible()){
										l.setVisible(false);
										if(hasError>0)
											hasError--;
									}
									if(!vcourses.contains(courseCode.getId().replace("subject",course.getCourseCode())))
										vcourses.add(courseCode.getId().replace("subject",course.getCourseCode()));
									for(int i=0; i<vprereqs.size(); i++){
										if(vprereqs.elementAt(i).contains(course.getCourseCode())){
											String s = (String) vprereqs.elementAt(i);
											String[] id = s.split("__");
											Label le = courseCode.getFellow(id[1].replace("subject", "error"));
											Label lpre = courseCode.getFellow(id[1].replace("subject", "prereq"));
											String[] date1 = id[1].split("_");
											int year1 = Integer.parseInt(date1[1]);
											int sem1 = Integer.parseInt(date1[2]);
											String prer =  prereqmap.get(courseCode.getFellow(id[1]).getValue().toUpperCase());
											
											if(!prer.contains(" AND ")){
												le.setVisible(false);
											}
											else{
												String[] ands = prer.split(" AND "); 
												boolean b = false;
												for(int j=0; j<ands.length; j++){
													if(ands[j].equals("SENIOR")){
														if(year1>=4)
															b=true;
														else{
															b=false;
															break;
														}
													}
													else if(ands[j].equals("JUNIOR")){
														if(year1>=3)
															b=true;
														else{
															b=false;
															break;
														}
													}
													else if(ands[j].equals("SOPHOMORE")){
														if(year1>=2)
															b=true;
														else{
															b=false;
															break;
														}
													}
													else{
														for(int k=0; k<vcourses.size(); k++){
															if(vcourses.elementAt(k).contains(ands[j]+"_")){
																String[] pdate = vcourses.elementAt(k).split("_");
																int pyear = Integer.parseInt(pdate[1]);
																int psem = Integer.parseInt(pdate[2]);
																if(pyear<year1 || (pyear==year&&psem<sem1)){
																	b = true;
																	break;
																}
																else
																	b = false;
															}
															else
																b = false;
														}
														if(!b)
															break;
													}
												}
												
												if(b){
													if(le.isVisible()){
														le.setVisible(false);
														if(hasError>0)
															hasError--;
													}
												}
											}
										}
									}
									
									String unitsId = courseCode.getId().replace("subject", "units");
									
									if(!course.getCourseCode().contains("NSTP")){
										total_units.setValue(Integer.parseInt(total_units.getValue())-Integer.parseInt(yearRow.getFellow(unitsId).getValue())+"");
										total_units.setValue(Integer.parseInt(total_units.getValue())+course.getUnits()+"");
										
										String[] semUnits = unitsId.split("_");
										Label sem_units = yearRow.getFellow(semUnits[1]+"_"+semUnits[2]);
										sem_units.setValue(Integer.parseInt(sem_units.getValue())-Integer.parseInt(yearRow.getFellow(unitsId).getValue())+"");
										sem_units.setValue(Integer.parseInt(sem_units.getValue())+course.getUnits()+"");
									}
									if(courseCode.getValue().toUpperCase().contains("(MST)")){
										total_MST.setValue(Integer.parseInt(total_MST.getValue())+1+"");
									}
									else if(courseCode.getValue().toUpperCase().contains("(AH)")){
										total_AH.setValue(Integer.parseInt(total_AH.getValue())+1+"");
									}
									else if(courseCode.getValue().toUpperCase().contains("(SSP)")){
										total_SSP.setValue(Integer.parseInt(total_SSP.getValue())+1+"");
									}
									else if(courseCode.getValue().toUpperCase().contains("ELECTIVE")){
										total_ELECTIVE.setValue(Integer.parseInt(total_ELECTIVE.getValue())+1+"");
									}
									yearRow.getFellow(unitsId).setValue(course.getUnits() + "");
								}
							}
						}
					}
				}
				else if(!oldVal.toUpperCase().equals(courseCode.getValue().toUpperCase()) || courseCode.getValue().equals("")){
					if(!l.isVisible()){
						l.setVisible(true);
						hasError++;
					}
				}
			}
		}
		
		void showInfoCurriculumCourse(int cId){
			int prevYear=0;
			int prevSem=0;
			int num=1;
			Integer totalUnits = 0;
			int totalAH;
			int totalMST;
			int totalSSP;
			int elective;
			
			
			List curriculumCourse = (new CurriculumCoursePeer()).getCurriculumCourses(cId);
			for(int i=0; i<curriculumCourse.size(); i++){
				int currentYear = curriculumCourse.get(i).getYear();
				if(currentYear!=prevYear){
					num=1;
					addYear();
					addSemester();
					String courseCode = curriculumCourse.get(i).getCourse().getCourseCode();
					yearRow.getFellow("subject_"+currentYear+"_1_1").setValue(courseCode);
					yearRow.getFellow("prereq_"+currentYear+"_1_1").setValue(grammar.Converter.prefixToInfix(curriculumCourse.get(i).getPrerequisite()));
					Integer credit = (new CoursePeer()).getCourse(courseCode).getUnits();
					yearRow.getFellow("units_"+currentYear+"_1_1").setValue(credit+"");
					if(!courseCode.contains("NSTP"))
						totalUnits += credit;
					prevSem = curriculumCourse.get(i).getSemester();
					prevYear = currentYear;
				}
				else{
					int currentSem = curriculumCourse.get(i).getSemester();
					if(prevSem!=currentSem){
						addSemester();
						num=1;
						prevSem=currentSem;
					}
					else{
						num++;
						addCourse(yearRow.getFellow("semester_"+currentYear+"_"+currentSem));
					}
					String courseCode = curriculumCourse.get(i).getCourse().getCourseCode();
					yearRow.getFellow("subject_"+currentYear+"_"+currentSem+"_"+num).setValue(courseCode);
					yearRow.getFellow("prereq_"+currentYear+"_"+currentSem+"_"+num).setValue(grammar.Converter.prefixToInfix(curriculumCourse.get(i).getPrerequisite()));
					Integer credit = (new CoursePeer()).getCourse(courseCode).getUnits();
					yearRow.getFellow("units_"+currentYear+"_"+currentSem+"_"+num).setValue(credit+"");
					if(!courseCode.contains("NSTP"))
						totalUnits += credit;
				}
				
			}
			Curriculum curri = (new CurriculumPeer()).getCurriculum(cId);
			total_AH.setValue(curri.getAHCount()+"");
			total_MST.setValue(curri.getMSTCount()+"");
			total_SSP.setValue(curri.getSSPCount()+"");
			total_ELECTIVE.setValue(curri.getElectiveCount()+"");
			total_units.setValue(curri.getTotalUnits()+"");
			gridYear.setActivePage(0);
		}
		
		void importCurriculumCourse(String curriculumCode){
			hasError=0;
			vcourses.clear();
			vprereqs.clear();
			prereqmap.clear();
			int currId=(new CurriculumPeer()).getCurriculumId(curriculumCode);
			List curriculumCourse = (new CurriculumCoursePeer()).getCurriculumCourses(currId);
			if(curriculumCourse.size()==0){
				alert(curriculumCode + " does not have any curriculum configuration yet.");
				return;
			}
			showInfo=false;
			int prevYear=0;
			int prevSem=0;
			int num=1;
			Integer totalUnits = 0;
			int totalAH;
			int totalMST;
			int totalSSP;
			Grid g = yearRow.getParent();
			Component c = g.getParent();
			c.removeChild(g);
			Grid gridYear = new Grid();
			gridYear.setMold("paging");
			gridYear.setId("gridYear");
			gridYear.setParent(c);
			gridYear.setPageSize(1);
			gridYear.setHeight("350px");
			gridYear.setWidth("568px");
			Rows yearRow = new Rows();
			yearRow.setParent(gridYear);
			yearRow.setId("yearRow");
			c.insertBefore(gridYear, refDiv);
		
			
			
			for(int i=0; i<curriculumCourse.size(); i++){
				int currentYear = curriculumCourse.get(i).getYear();
				if(currentYear!=prevYear){
					num=1;
					addYear();
					addSemester();
					
					String courseCode = curriculumCourse.get(i).getCourse().getCourseCode();
					yearRow.getFellow("subject_"+currentYear+"_1_1").setValue(courseCode);
					String prereq = grammar.Converter.prefixToInfix(curriculumCourse.get(i).getPrerequisite());
					if(!prereq.equals("(DUMMY)"))
						yearRow.getFellow("prereq_"+currentYear+"_1_1").setValue(prereq);
					if(!(courseCode.equals("GE(MST)") || courseCode.equals("GE(SSP)") || courseCode.equals("GE(AH)") || courseCode.equals("ELECTIVE")))
						vcourses.add(courseCode+"_"+currentYear+"_1_1");
					if(! (prereq.equals("(DUMMY)") || prereq.equals("NONE") || prereq.equals("GRADUATE"))){
						String[] prereqs = extractPrereq(prereq);
						boolean satisfied=false;
						boolean coiAnd=false;
						if(prereq.contains("COI")){
							String coi = new String();
							for(int i=0; i<prereqs.length; i++){
								if(prereqs[i].contains("COI")){
									coi = prereqs[i];
									break;
								}
							}
							if(!coi.contains(" AND ")){
								satisfied=true;
							}
							else
								coiAnd=true;
						}
						else{
							coiAnd=true;
						}
						if(coiAnd){
							for(int i=0; i<prereqs.length; i++){
								if(!prereqs[i].contains(" AND ")){
									if(prereqs[i].equals("SENIOR")  && currentYear>=4){
										break;
									}
									else if(prereqs[i].equals("JUNIOR") && currentYear>=3){
										break;
									}
									else if(prereqs[i].equals("SOPHOMORE") && currentYear>=2){
										break;
									}
									else{
										for(int j=0; j<vcourses.size(); j++){
											if(vcourses.elementAt(j).contains(prereqs[i]+"_")){
												if(!vprereqs.contains(prereqs[i]+"__"+"subject_"+currentYear+"_1_1"))
													vprereqs.add(prereqs[i]+"__"+"subject_"+currentYear+"_1_1");
												satisfied=true;
												break;
											}
										}
										if(satisfied){
											prereqmap.put(courseCode,prereqs[i]);
											break;
										}
									}
								}
								else{
									String[] ands = prereqs[i].split(" AND "); 
									boolean b = false;
									for(int j=0; j<ands.length; j++){
										if(ands[j].equals("SENIOR")){
											if(currentYear>=4)
												b=true;
											else{
												b=false;
												break;
											}
										}
										else if(ands[j].equals("JUNIOR")){
											if(currentYear>=3)
												b=true;
											else{
												b=false;
												break;
											}
										}
										else if(ands[j].equals("SOPHOMORE")){
											if(currentYear>=2)
												b=true;
											else{
												b=false;
												break;
											}
										}
										else if(ands[j].equals("COI")){
											b=true;
										}
										else{
											for(int k=0; k<vcourses.size(); k++){
												if(vcourses.elementAt(k).contains(ands[j]+"_")){
													b = true;
													break;
												}
												else
													b = false;
											}
											if(!b)
												break;
										}
									}
									if(b){
										satisfied=true;
										
										for(int j=0; j<ands.length; j++){
											if(!vprereqs.contains(ands[j]+"__"+"subject_"+currentYear+"_1_1") && !(ands[j].equals("SENIOR") || ands[j].equals("JUNIOR") || ands[j].equals("SOPHOMORE") || ands[j].equals("COI")))
												vprereqs.add(ands[j]+"__"+"subject_"+currentYear+"_1_1");
										}
										prereqmap.put(courseCode,prereqs[i]);
										break;
									}
								}
							}
						}
					}
					Integer credit = (new CoursePeer()).getCourse(courseCode).getUnits();
					yearRow.getFellow("units_"+currentYear+"_1_1").setValue(credit+"");
					if(!courseCode.contains("NSTP"))
						totalUnits += credit;
					yearRow.getFellow(currentYear+"_1").setValue((Integer.parseInt(yearRow.getFellow(currentYear+"_1").getValue())+credit)+"");
					prevSem = curriculumCourse.get(i).getSemester();
					prevYear = currentYear;
				}
				else{
					int currentSem = curriculumCourse.get(i).getSemester();
					if(prevSem!=currentSem){
						addSemester();
						yearRow.getFellow(currentYear+"_"+prevSem).getParent().getParent().getFirstChild().detach();
						num=1;
						prevSem=currentSem;
					}
					else{
						num++;
						addCourse(yearRow.getFellow("semester_"+currentYear+"_"+currentSem));
					}
					String courseCode = curriculumCourse.get(i).getCourse().getCourseCode();
					String prereq = grammar.Converter.prefixToInfix(curriculumCourse.get(i).getPrerequisite());
					yearRow.getFellow("subject_"+currentYear+"_"+currentSem+"_"+num).setValue(courseCode);
					yearRow.getFellow("prereq_"+currentYear+"_"+currentSem+"_"+num).setValue(prereq);
					if(!(courseCode.equals("GE(MST)") || courseCode.equals("GE(SSP)") || courseCode.equals("GE(AH)") || courseCode.equals("ELECTIVE")))
						vcourses.add(courseCode+"_"+currentYear+"_"+currentSem+"_"+num);
					if(! (prereq.equals("(DUMMY)") || prereq.equals("NONE") || prereq.equals("GRADUATE"))){
						String[] prereqs = extractPrereq(prereq);
						boolean satisfied=false;
						boolean coiAnd=false;
						if(prereq.contains("COI")){
							String coi = new String();
							for(int i=0; i<prereqs.length; i++){
								if(prereqs[i].contains("COI")){
									coi = prereqs[i];
									break;
								}
							}
							if(!coi.contains(" AND ")){
								satisfied=true;
							}
							else
								coiAnd=true;
						}
						else{
							coiAnd=true;
						}
						if(coiAnd){
							for(int i=0; i<prereqs.length; i++){
								if(!prereqs[i].contains(" AND ")){
									if(prereqs[i].equals("SENIOR")  && currentYear>=4){
										break;
									}
									else if(prereqs[i].equals("JUNIOR") && currentYear>=3){
										break;
									}
									else if(prereqs[i].equals("SOPHOMORE") && currentYear>=2){
										break;
									}
									else{
										for(int j=0; j<vcourses.size(); j++){
											if(vcourses.elementAt(j).contains(prereqs[i]+"_")){
												if(!vprereqs.contains(prereqs[i]+"__"+"subject_"+currentYear+"_"+currentSem+"_"+num))
													vprereqs.add(prereqs[i]+"__"+"subject_"+currentYear+"_"+currentSem+"_"+num);
												satisfied=true;
												break;
											}
										}
										if(satisfied){
											prereqmap.put(courseCode,prereqs[i]);
											break;
										}
									}
								}
								else{
									String[] ands = prereqs[i].split(" AND "); 
									boolean b = false;
									for(int j=0; j<ands.length; j++){
										if(ands[j].equals("SENIOR")){
											if(currentYear>=4)
												b=true;
											else{
												b=false;
												break;
											}
										}
										else if(ands[j].equals("JUNIOR")){
											if(currentYear>=3)
												b=true;
											else{
												b=false;
												break;
											}
										}
										else if(ands[j].equals("SOPHOMORE")){
											if(currentYear>=2)
												b=true;
											else{
												b=false;
												break;
											}
										}
										else if(ands[j].equals("COI")){
											b=true;
										}
										else{
											for(int k=0; k<vcourses.size(); k++){
												if(vcourses.elementAt(k).contains(ands[j]+"_")){
													b = true;
													break;
												}
												else
													b = false;
											}
											if(!b)
												break;
										}
									}
									if(b){
										satisfied=true;
										
										for(int j=0; j<ands.length; j++){
											if(!vprereqs.contains(ands[j]+"__"+"subject_"+currentYear+"_1_1") && !(ands[j].equals("SENIOR") || ands[j].equals("JUNIOR") || ands[j].equals("SOPHOMORE") || ands[j].equals("COI")))
												vprereqs.add(ands[j]+"__"+"subject_"+currentYear+"_"+currentSem+"_"+num);
										}
										prereqmap.put(courseCode,prereqs[i]);
										break;
									}
								}
							}
						}
					}
					
					Integer credit = (new CoursePeer()).getCourse(courseCode).getUnits();
					yearRow.getFellow("units_"+currentYear+"_"+currentSem+"_"+num).setValue(credit+"");
					if(!courseCode.contains("NSTP"))
						totalUnits += credit;
					yearRow.getFellow(currentYear+"_"+currentSem).setValue((Integer.parseInt(yearRow.getFellow(currentYear+"_"+currentSem).getValue())+credit)+"");
				}
				
			}
			Curriculum curri = (new CurriculumPeer()).getCurriculum(currId);
			total_AH.setValue(curri.getAHCount()+"");
			total_MST.setValue(curri.getMSTCount()+"");
			total_SSP.setValue(curri.getSSPCount()+"");
			total_ELECTIVE.setValue(curri.getElectiveCount()+"");
			total_units.setValue(totalUnits+"");
			gridYear.setActivePage(0);
			buttons.setVisible(true);
		}
		
		
		void filterCurriculumCode(String string){
			ListIterator i = curricula.listIterator();
			List list = new java.util.Vector();
			while(i.hasNext()){
				String temp = i.next().getCurriculumCode(); 
				if(temp.toUpperCase().startsWith(string.toUpperCase())){
					list.add(temp);
				}
			}
			curriculum_lb.getModel().clear();
			curriculum_lb.getModel().addAll(list);
		}
		
		void retrieveCurricula(){
			int dId = (new CurriculumPeer()).getCurriculum(cId).getDegreeId();
			curricula = (new CurriculumPeer()).getCurriculumByDegreeCourse(dId);
			String[] listModel = new String[curricula.size()];
			for (int i=0; i<curricula.size(); i++)
				listModel[i] = curricula.get(i).getCurriculumCode();
			ListModel strset = new ListModelList(listModel);
			curriculum_lb.setModel(strset);
			curriculum_lb.setItemRenderer(new UIItemRenderer());
			 
		}